package tt.dz.order.vo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import tt.dz.order.exception.ParamLackException;
import tt.dz.order.exception.ParamNotFoundInSystemException;
import tt.dz.order.pojo.OrderInfo;
import tt.dz.order.pojo.OrderItem;
import tt.dz.order.pojo.OrderServiceInfo;

/**
 * @Title 订单基础数据,OMS调用订单系统接口传过来
 * @author zhoutian   
 * @date 2015-11-26
 */
public class OrderBaseData {
	//itemid 用于订单金额修改定位
	private transient String itemId;
	private transient String userId;
	//用户类型，0 个人用户，1 企业账户,2 支持统付的个人客户
	private transient String userType;
	//卡号
	private String cardNo;
	//卡类型 08 虚拟，06 实体卡  ，07  身份识别卡
	private String cardType;
	//卡上的余额，如果卡类型是"06 线下卡",需要OMS在第1次调用时传递卡上的余额
	private String payerBalance;
	//来源系统,用户使用什么app下的单：104 自主app, 201乐视
	private String appId;
	//用户所属运营商编码
	private transient String merchantCode;
	//服务编号
	private String serviceNo;
	//资源（桩）所属运营商编号
	private String serviceProviderCode;
	//该运营商提供的服务是否是成本类的服务，0：否 1：是
	private transient int isCost;
	//计量单位编号
	private transient int unitNo;
	//电站Id
	private String plantId;
	//桩的编号
	private String equipmentNo;
	//辅助设备编号,枪的编号
	private String auxEquipmentNo;
	//单价，oms系统保存的原始单价
	private BigDecimal unitPrice;
	//总价，如电费
	private BigDecimal totalPrice;
	//购买数量，如预约时间、电量等
	private BigDecimal buyNum;
	//服务开始时间
	private Date startTime;
	//服务结束时间
	private Date endTime;
	//支付状态
	private Integer payStatus;
	//订单项描述信息
	private String message;
	//支付类别
	private Integer payType;
	//白名单用户使用合作建设资源的打折信息
	private WhiteListInfo whiteListInfo;
	//分润规则信息
	private DivideInfo divideInfo;
	
	public void validate() throws ParamLackException, ParamNotFoundInSystemException{
		//查询所有服务的服务号和服务名称
		Map<String,String> orderOrderServiceInfoMap = OrderServiceInfo.getAllServiceCodeAndName();
		if(StringUtils.isBlank(cardType)){
			throw new ParamLackException("cardType为空");
		}
		if(StringUtils.isBlank(cardNo)){
			throw new ParamLackException("cardNo为空");
		}
		if(StringUtils.isBlank(appId)){
			throw new ParamLackException("appId为空");
		}
		if(StringUtils.isBlank(serviceNo)){
			throw new ParamLackException("serviceNo为空");
		}
		if(!orderOrderServiceInfoMap.containsKey(serviceNo)){
			throw new ParamNotFoundInSystemException("serviceNo为空");
		}
		if(StringUtils.isBlank(serviceProviderCode)){
			throw new ParamLackException("serviceProviderCode为空");
		}
		if(StringUtils.isBlank(plantId)){
			throw new ParamLackException("plantId为空");
		}
		if(StringUtils.isBlank(equipmentNo)){
			throw new ParamLackException("equipmentNo为空");
		}
		if(StringUtils.isBlank(auxEquipmentNo)){
			throw new ParamLackException("auxEquipmentNo为空");
		}
		if(unitPrice == null && totalPrice == null){
			throw new ParamLackException("unitPrice和totalPrice为空");
		}
		if(buyNum == null){
			throw new ParamLackException("buyNum为空");
		}
		if(startTime == null){
			throw new ParamLackException("startTime为空");
		}
		if(endTime == null){
			throw new ParamLackException("endTime为空");
		}
		if(payStatus == null){
			throw new ParamLackException("payStatus为空");
		}
		if(whiteListInfo != null){
			whiteListInfo.validate();
		}
		if(divideInfo != null){
			divideInfo.validate();
		}
	}
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserType() {
		return userType;
	}

	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	public String getCardType() {
		return cardType;
	}

	public void setCardType(String cardType) {
		this.cardType = cardType;
	}

	public String getPayerBalance() {
		return payerBalance;
	}

	public void setPayerBalance(String payerBalance) {
		this.payerBalance = payerBalance;
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	public String getServiceNo() {
		return serviceNo;
	}

	public void setServiceNo(String serviceNo) {
		this.serviceNo = serviceNo;
	}

	public String getServiceProviderCode() {
		return serviceProviderCode;
	}

	public void setServiceProviderCode(String serviceProviderCode) {
		this.serviceProviderCode = serviceProviderCode;
	}

	public int getIsCost() {
		return isCost;
	}

	public void setIsCost(int isCost) {
		this.isCost = isCost;
	}

	public int getUnitNo() {
		return unitNo;
	}

	public void setUnitNo(int unitNo) {
		this.unitNo = unitNo;
	}

	public String getEquipmentNo() {
		return equipmentNo;
	}

	public void setEquipmentNo(String equipmentNo) {
		this.equipmentNo = equipmentNo;
	}

	public String getAuxEquipmentNo() {
		return auxEquipmentNo;
	}

	public void setAuxEquipmentNo(String auxEquipmentNo) {
		this.auxEquipmentNo = auxEquipmentNo;
	}

	public String getPlantId() {
		return plantId;
	}

	public void setPlantId(String plantId) {
		this.plantId = plantId;
	}

	public BigDecimal getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public BigDecimal getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(BigDecimal totalPrice) {
		this.totalPrice = totalPrice;
	}

	public BigDecimal getBuyNum() {
		return buyNum;
	}

	public void setBuyNum(BigDecimal buyNum) {
		this.buyNum = buyNum;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	
	public Integer getPayStatus() {
		return payStatus;
	}

	public void setPayStatus(Integer payStatus) {
		this.payStatus = payStatus;
	}

	public Integer getPayType() {
		return payType;
	}

	public void setPayType(Integer payType) {
		this.payType = payType;
	}

	public WhiteListInfo getWhiteListInfo() {
		return whiteListInfo;
	}

	public void setWhiteListInfo(WhiteListInfo whiteListInfo) {
		this.whiteListInfo = whiteListInfo;
	}

	public DivideInfo getDivideInfo() {
		return divideInfo;
	}

	public void setDivideInfo(DivideInfo divideInfo) {
		this.divideInfo = divideInfo;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public OrderBaseData() {}
	
	/**
	 * 用订单基础数据构造订单项里的部分数据
	 * @param orderBaseData 订单基础数据
	 */
	public OrderBaseData(OrderInfo orderInfo,OrderItem orderItem) {
		this.userId = orderItem.getUserId();
		this.serviceNo = orderItem.getServiceNo();
		this.serviceProviderCode = orderItem.getServiceProvider();
		this.unitNo = orderItem.getUnitNo();
		this.unitPrice = orderItem.getUnitPrice();
		this.unitPrice = orderItem.getUnitPrice();
		this.buyNum = orderItem.getBuyNum();
		this.totalPrice = orderItem.getTotalPrice();
		this.plantId = orderItem.getServiceStationNo();
		this.equipmentNo = orderItem.getEquipmentNo();
		this.auxEquipmentNo = orderItem.getAuxEquipmentNo();
		this.startTime = orderItem.getStartTime();
		this.endTime = orderItem.getEndTime();
		this.message = orderItem.getMessage();
		this.userType=orderInfo.getUserType();
	}
	
	/*
	public static void main(String[] args){
		String str = "{\"bizData\":[{\"createTime\":\"2015-12-24 17:30:00\",\"isCanUse\":1,\"isValid\":1,\"isVirtual\":0,\"name\":\"充电\",\"parentNo\":\"#\",\"serviceNo\":\"100\"},{\"createTime\":\"2015-12-24 17:30:00\",\"isCanUse\":1,\"isValid\":1,\"isVirtual\":0,\"name\":\"充电服务\",\"parentNo\":\"100\",\"serviceNo\":\"100001\"},{\"createTime\":\"2015-12-24 17:30:00\",\"isCanUse\":1,\"isValid\":1,\"isVirtual\":0,\"name\":\"代收电费服务\",\"parentNo\":\"100\",\"serviceNo\":\"100002\"},{\"createTime\":\"2015-12-24 17:30:00\",\"isCanUse\":1,\"isValid\":1,\"isVirtual\":0,\"name\":\"预约\",\"parentNo\":\"#\",\"serviceNo\":\"200\"},{\"createTime\":\"2015-12-24 17:30:00\",\"isCanUse\":1,\"isValid\":1,\"isVirtual\":0,\"name\":\"充电预约服务\",\"parentNo\":\"200\",\"serviceNo\":\"200001\"},{\"createTime\":\"2015-12-24 17:30:00\",\"isCanUse\":1,\"isValid\":1,\"isVirtual\":0,\"name\":\"充电预约退款服务\",\"parentNo\":\"200\",\"serviceNo\":\"200002\"},{\"createTime\":\"2015-12-24 17:30:00\",\"isCanUse\":1,\"isValid\":1,\"isVirtual\":0,\"name\":\"租车\",\"parentNo\":\"#\",\"serviceNo\":\"300\"}],\"resultCode\":\"1020300\",\"retCode\":\"success\"}";
		RetVO retVO = (RetVO) JSON.parseObject(str, RetVO.class);
		String bizData = (String) retVO.getBizData();
		List<OrderServiceInfo> list = JSON.parseArray(bizData, OrderServiceInfo.class);
		System.out.println(list.size());
	}
	*/
}
